"""Header_Information
------------------------------------
    File_Name: exceptions_handlers
    Author: RR9
    Date: 2024/09/26
    Description: 异常处理器
------------------------------------
"""
from fastapi import Request, responses
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from starlette import status
from starlette.responses import JSONResponse
from app.exception.exceptions import CustomException


async def custom_exception_handler(request: Request, exc: CustomException):
    return responses.JSONResponse(
        status_code=499,
        content={"message": f"""99999-- {exc.message} --999999"""}
    )


# 覆盖默认请求验证异常
"""
FastAPI HTTPException vs Starlette HTTPException
FastAPI HTTPException 继承自 Starlette HTTPException
两者唯一区别是：FastAPI 的 HTTPException 可以在响应中添加响应头

"""


async def custom_validation_exception_handler(request: Request, exc: RequestValidationError):
    """ 默认数据校验失败信息，额外添加用户请求数据body """
    return JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content=jsonable_encoder({"detail": exc.errors(), "body": exc.body})
    )
